package com.dyw.spzx.manager.service.lmpl;

import com.dyw.spzx.common.excepiton.GuiguException;
import com.dyw.spzx.manager.helper.MenuHelper;
import com.dyw.spzx.manager.mapper.SysMenuMapper;
import com.dyw.spzx.manager.mapper.SysRoleMenuMapper;
import com.dyw.spzx.manager.service.SysMenuService;
import com.dyw.spzx.model.vo.system.SysMenuVo;
import com.dyw.spzx.model.entity.system.SysMenu;
import com.dyw.spzx.model.entity.system.SysUser;
import com.dyw.spzx.model.vo.common.ResultCodeEnum;
import com.dyw.spzx.util.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    // 获取所有菜单树形结构数据
    @Override
    public List<SysMenu> findNodes() {
        // 查询所有的菜单
        List<SysMenu> sysMenuList = sysMenuMapper.selectAllSysmenu();
        // 封装树形结构
        if (CollectionUtils.isEmpty(sysMenuList)) {
            return null;
        }
        List<SysMenu> treeSysMenuList = MenuHelper.buildTree(sysMenuList);

        return treeSysMenuList;
    }

    // 添加菜单
    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.insertSysMenu(sysMenu);

        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开
        updateSysRoleMenuIsHalf(sysMenu);

    }

    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {
        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuMapper.selectByParentId(sysMenu.getParentId());
        if (parentMenu != null) {
            // 将该id的菜单设置为半开
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;

            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu) ;
        }
    }


    // 修改菜单
    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
    }

    // 删除菜单
    @Override
    public void removeById(Long id) {
        // 查询当前菜单是否有子菜单
        int count = sysMenuMapper.countByParentId(id);
        if (count > 0) {
            throw new GuiguException(ResultCodeEnum.NODE_ERROR);
        }
        sysMenuMapper.deleteSysMenuById(id);

    }

    // 动态菜单
    @Override
    public List<SysMenuVo> findUserMenuList() {
        // 获取当前登录用户的ID
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();
        List<SysMenu> sysMenuList = sysMenuMapper.selectMenuListByUserId(userId);
        // 构建树形数据
        List<SysMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);
        List<SysMenuVo> sysMenuVoList = buildMenus(sysMenuTreeList);
        return sysMenuVoList;
    }

    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {
        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }


}
